#include <accctrl.h>
#include <Aclapi.h>
class wSAEveryone : public SECURITY_ATTRIBUTES
{
public:
	wSAEveryone() 
	{
		SECURITY_ATTRIBUTES::nLength = sizeof (SECURITY_ATTRIBUTES);
		SECURITY_ATTRIBUTES::lpSecurityDescriptor = NULL;
		SECURITY_ATTRIBUTES::bInheritHandle = FALSE;
		
		m_pEveryoneSID = NULL;
		m_pACL = NULL;
		m_pSD = NULL;
		
		InitEveryoneSA();
	}
	~wSAEveryone()
	{
		if(m_pEveryoneSID != NULL)
			FreeSid(m_pEveryoneSID);
		if(m_pACL!= NULL)
			LocalFree(m_pACL);
		if(m_pSD != NULL)
			LocalFree(m_pSD);
	}
private:
	BOOL InitEveryoneSA()
	{
		SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY;
		if(!AllocateAndInitializeSid( &SIDAuthWorld, 1
			, SECURITY_WORLD_RID
			, 0, 0, 0, 0, 0, 0, 0
			, &m_pEveryoneSID))
			return FALSE;
		
		EXPLICIT_ACCESS ea = {0};
		ea.grfAccessPermissions = MUTEX_ALL_ACCESS;
		ea.grfAccessMode = SET_ACCESS;
		ea.grfInheritance = NO_INHERITANCE;
		ea.Trustee.TrusteeForm = TRUSTEE_IS_SID;
		ea.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
		ea.Trustee.ptstrName = (LPTSTR) m_pEveryoneSID;
		
		if (ERROR_SUCCESS != SetEntriesInAcl(1, &ea, NULL, &m_pACL))
			return FALSE;
		m_pSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
		if (m_pSD == NULL || !InitializeSecurityDescriptor(m_pSD, SECURITY_DESCRIPTOR_REVISION))
			return FALSE;
		
		// Add the ACL to the security descriptor.
		if (!SetSecurityDescriptorDacl(m_pSD, TRUE, m_pACL, FALSE))   // not a default DACL
			return FALSE;
		
		// Initialize a security attributes structure.
		SECURITY_ATTRIBUTES::nLength = sizeof (SECURITY_ATTRIBUTES);
		SECURITY_ATTRIBUTES::lpSecurityDescriptor = m_pSD;
		SECURITY_ATTRIBUTES::bInheritHandle = FALSE;
		return TRUE;
	}
private:
	PSID
		m_pEveryoneSID;
	PACL
		m_pACL;
	PSECURITY_DESCRIPTOR m_pSD;
};